<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class User extends Auth
{

    private $db;
    public function __construct()
    {
        parent::__construct();
        $this->db=new \app\admin\model\user;
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $field=$this->db->getAll();
        $this->assign('field',$field);
        return view();

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function user_store()
    {
        if(session('id')!=1){
            $this->error('没有权限');
        }

        if(Request::instance()->isPost()){
            $data=input('post.');
            $re=$this->db->store($data);
            if(!$re['code']){
                $this->error($re['message']);die;
            }
            $this->success($re['message']);die;
        }
        $where['status']=1;
        $groupData=db( 'auth_group' )->where($where)->order('id desc')->field('id,title')->select();
        $this->assign('groupData',$groupData);
        return view();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function lock_user($id)
    {
        if($id==1){
            return json_encode(array('code'=>0,'message'=>'管理员不能关闭'));die;
        }
        $data['is_lock']=1;
        $re=$this->db->where(['id'=>$id])->update($data);
        if(!$re){
            return json_encode(array('code'=>0,'message'=>'error'));die;
        }
        return json_encode(array('code'=>1,'message'=>'success'));die;
    }


    /**
     * @info 修改密码
     * @return \think\response\View
     */
    public function pass(){
        if(Request::instance()->isPost()){
            $data=input('post.');
            if($data['newpassword']!=$data['newpassword2']){
                $this->error('两次密码不一至');die;
            }
            $re=$this->db->editpass($data);
            if(!$re){
                $this->error($this->db->getError());die;
            }
            $this->success('修改成功',url('index/index'));
        }
        return view();
    }

    /**
     * @info 搜索用户
     */
    public function search(){
        if(Request::instance()->isPost()){
            $phone=input('post.search','');
            if(!$phone){
                $this->error('请输入搜索的手机号');
            }

            $where['username']=$phone;
            $data=$this->db->where($where)->paginate(1);
            $this->assign('field',$data);
            return view('index');
        }
    }


    /**
     * @info 重置密码
     */
    public function resetpass(){

        if(session('id')!=1){
            $this->error('没有权限');
        }
        $id=input('get.id',0,'intval');
//        var_dump($id);die;
        $where['id']=$id;
        $oneData=$this->db->where($where)->find();

        $username=$oneData['username'];
        $password=md5(substr($username,-6,6));
        $re=$this->db->where($where)->update(['password'=>$password]);
        if(!$re){
            $this->error('重置失败');
        }
        $this->success('重置成功');
    }

    /**
     * 删除
     */
    public function user_del(){
        $id=input('post.id',0,'intval');
//        var_dump($id);die;
        if($id==1){
            returnMsg(0,'permission error');
        }
        $where['id']=$id;
        $re=db('user')->where($where)->delete();
        if(!$re){
            returnMsg(0,'delete error');
        }
        returnMsg(1,'success');
    }


}
